import { FUDAI, TIMES } from '@/config';
import LogManager from '@/utils/LogManager';
import SettingsManager from '@config/settings';
import Fudai from './Fudai';
import LiveRoom from './LiveRoom';

export function launchDouyin() {
  const settings = SettingsManager.getSettings();
  switch (settings.scope) {
    case 'all':
      LiveRoom.enterAllLive();
      break;
    case 'followed':
      LiveRoom.enterConcernLive();
      break;
  }
}

export function start() {
  while (true) {
    const settings = SettingsManager.getSettings();
    // 画质是否切换成功
    /* if (!LiveRoom.imageQuality) {
      return;
    } */

    // 直播已结束
    if (LiveRoom.isEnd) {
      if (settings.scope === 'followed') {
        back();
        sleep(TIMES.LIVEROOM_SLEEP_LONE);
        LiveRoom.swipeToLive('上'); // 刷新
        sleep(TIMES.LIVEROOM_SLEEP_LONE);
        LiveRoom.enterConcernLive();
        continue;
      }
      LiveRoom.swipeToLive(LiveRoom.direction);
      continue;
    }

    // 未在直播间内
    const isLive = LiveRoom.isLive;
    if (!isLive && currentActivity() !== 'com.bytedance.android.annie.dialog.NoMarginSheetBaseDialog') {
      LogManager.add('请进入直播间');
      continue;
    }

    // 关注列表直播滑动到底处理
    if (settings.scope === 'followed') {
      if (LiveRoom.getRecordLive.switchCount === 2 && !LiveRoom.isSwitchover) {
        LiveRoom.direction = '下';
        LiveRoom.setRecordLive('switchCount', 0);
      }

      if (text('关注').findOne(TIMES.LIVEROOM_FIND_NODE_TIMEOUT_SHORT)) {
        LiveRoom.direction = '上';
        LiveRoom.setRecordLive('switchCount', 0);
      }
    }

    // 切换直播间
    if (LiveRoom.isSwitchover && currentActivity() !== 'com.bytedance.android.annie.dialog.NoMarginSheetBaseDialog') {
      Fudai.setStatus(false);
      LiveRoom.updateCurrentLive();
      LogManager.add(JSON.stringify(LiveRoom.getRecordLive));
      continue;
    }

    // 福袋结果处理
    if (Fudai.resultNode) {
      Fudai.clickResult();
      if (settings.scope === 'fixed') {
        continue;
      }
      LiveRoom.swipeToLive(LiveRoom.direction);
      continue;
    }

    // 福袋已参与
    if (Fudai.getStatus()) {
      continue;
    }

    // 打开福袋节点*秒未找到
    if (Fudai.getOpenNodeTimeoutCount() >= FUDAI.VOID_OPEN_NODE_COUND) {
      if (settings.scope === 'fixed') {
        LiveRoom.swipeToLive('下');
        sleep(TIMES.LIVEROOM_SLEEP_LONE);
        LiveRoom.swipeToLive('上');
        sleep(TIMES.LIVEROOM_SLEEP_SHORT);
        Fudai.setOpenNodeTimeoutCount(0);
        continue;
      } else {
        LiveRoom.swipeToLive(LiveRoom.direction);
        Fudai.setOpenNodeTimeoutCount(0);
        continue;
      }
    }

    // 在福袋参与界面
    if (Fudai.participateNode) {
      sleep(TIMES.FUDAI_SLEEP_LONE);
      back();
      continue;
    }

    // 抢固定直播间
    if (settings.scope === 'fixed') {
      // 时间 不满足切换直播间
      const [minute, second] = Fudai.countDown;
      if (minute === -1) {
        continue;
      }
      if (minute === 0 && second < FUDAI.JION_MIN_TIME) {
        LogManager.add('时间 不满足等待福袋倒计时');
        continue;
      }
      Fudai.clickOpen();
      sleep(TIMES.FUDAI_SLEEP_SHORT);
      Fudai.clickParticipate();
      continue;
    }

    // 人数 不满足切换直播间
    if (LiveRoom.getRecordLive.people < 8000) {
      LiveRoom.swipeToLive(LiveRoom.direction);
      LogManager.add('人数 不满足切换直播间');
      continue;
    }

    // 时间 不满足切换直播间
    const [minute, second] = Fudai.countDown;
    if (minute === -1) {
      continue;
    }
    if (minute > settings.triggerMinutes || (minute === 0 && second < FUDAI.JION_MIN_TIME)) {
      LiveRoom.swipeToLive(LiveRoom.direction);
      LogManager.add(`[时间不满足] 要求时间: ${settings.triggerMinutes}, 实际时间: ${minute}}`);
      continue;
    }

    // 心仪物品 是否满足
    if (!Fudai.isFavoredDescribe) {
      back();
      sleep(1000);
      LiveRoom.swipeToLive(LiveRoom.direction);
      LogManager.add('心仪物品 不满足切换直播间');
      continue;
    } else {
      // 价格 是否满足
      if (Fudai.price >= settings.minValue && Fudai.price < settings.maxValue) {
        Fudai.clickParticipate();
        continue;
      }
      LogManager.add('价格 不满足切换直播间');
    }
  }
}
